﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TsCore;
using System.Globalization;

namespace TsWeb.Matches
{
    public partial class EditMatch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int MatchId = Int32.Parse(Request.QueryString["MatchId"].ToString());
                initializeFields(MatchId);
            }
        }


        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //                ddl_sports.SelectedIndex = 0;
            Int32 sport = Int32.Parse(ddl_sports.SelectedItem.Value);
            DDL1_teamHome.DataSource = TsSystem.Instance.GetTeamsForSport(sport);
            DDL1_teamHome.DataBind();
            DDL2_teamVisitor.DataSource = TsSystem.Instance.GetTeamsForSport(sport);
            DDL2_teamVisitor.DataBind();
            //ModalPopupExtender1.Show(); OJO ACA

        }


        private void initializeFields(int MatchKey)
        {
            Match match = TsSystem.Instance.GetMatch(MatchKey);
            Int32 sport = TsSystem.Instance.GetTeam(match.HomeTeam.TeamId).Sport.SportId;
            string txtsport = TsSystem.Instance.GetTeam(match.HomeTeam.TeamId).Sport.Name;

            Session["UpdatedMatch"] = match;
            ddl_sports.DataSource = TsSystem.Instance.GetAllSports();
            ddl_sports.DataBind();
            ddl_sports.SelectedValue = ddl_sports.Items.FindByText(txtsport).Value;

            txtDate.Text = match.Date.ToString();

            DDL1_teamHome.DataSource = TsSystem.Instance.GetTeamsForSport(sport);
            DDL1_teamHome.DataBind();
            DDL1_teamHome.SelectedValue = DDL1_teamHome.Items.FindByText(match.HomeTeam.Name).Value; // match.HomeTeam.Name;

            DDL2_teamVisitor.DataSource = TsSystem.Instance.GetTeamsForSport(sport);
            DDL2_teamVisitor.DataBind();
            DDL2_teamVisitor.SelectedValue = DDL2_teamVisitor.Items.FindByText(match.VisitorTeam.Name).Value; //match.VisitorTeam.Name;

            ddl_winner.Items.Clear();
            ddl_winner.Items.Add(new ListItem(match.VisitorTeam.Name, match.VisitorTeam.TeamId.ToString()));
            ddl_winner.Items.Add(new ListItem(match.HomeTeam.Name, match.HomeTeam.TeamId.ToString()));
            ddl_winner.Items.Add(new ListItem("Empate", "0"));
            ddl_winner.Items.Add(new ListItem("No jugado", "-1"));
            ddl_winner.Items.FindByValue(match.Winner.ToString()).Selected = true;
        }

        protected void UpdateMatchButton_Click(object sender, EventArgs e)
        {
            Match updatedMatch = (Match)Session["UpdatedMatch"];
            Team home = TsSystem.Instance.GetTeam(Convert.ToInt32(DDL1_teamHome.SelectedValue));
            Team visitor = TsSystem.Instance.GetTeam(Convert.ToInt32(DDL2_teamVisitor.SelectedValue));
            Int32 winner = Convert.ToInt32(ddl_winner.SelectedValue);

            updatedMatch.HomeTeam = home;
            updatedMatch.VisitorTeam = visitor;
            updatedMatch.Date = Convert.ToDateTime(txtDate.Text, CultureInfo.CurrentCulture); ;
            updatedMatch.Winner = winner;
            TsSystem.Instance.updateMatch(updatedMatch);

            Response.Redirect("~/Matches/ManageMatches.aspx");

        }


    }
}